package com.zqw.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqw.info.OrderInfo;
import com.zqw.info.ResponseMsg;
import com.zqw.pojo.*;
import com.zqw.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    //跳转到订单界面
    @RequestMapping("orderList")
    public String orderList(OrderInfo orderInfo, HttpServletRequest request){
        PageHelper.startPage(orderInfo.getPageNum(),orderInfo.getPageSize());
        List<OrderPojo> orderPojos = orderService.queryOrderss(orderInfo);
        PageInfo<OrderPojo> pageInfo = new PageInfo<>(orderPojos);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("oid",orderInfo.getOid());
        request.setAttribute("ostatus",orderInfo.getOstatus());
        return "order-list";
    }

    //删除订单
    @RequestMapping("delOrder")
    @ResponseBody
    public ResponseMsg delOrder(String oid, HttpServletRequest request){
        ResponseMsg rm = new ResponseMsg();
        String[] oids;
        if (oid.contains(",")){
            oids = oid.split(",");
        }else{
            oids = new String[]{oid};
        }
        boolean b = orderService.delOrder(oids);
        if (b){
            rm.setResult("0000");
        }else{
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //跳转到修改订单界面
    @RequestMapping("orderEdit")
    public String orderEdit(String oid,HttpServletRequest request){
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOid(oid);
        List<OrderPojo> orderPojos = orderService.queryOrderss(orderInfo);
        if(orderPojos!=null&&orderPojos.size()>0){
            request.setAttribute("order",orderPojos.get(0));
            return "order-edit";
        }else{
            return "error";
        }
    }

    //修改订单
    @RequestMapping("updOrder")
    @ResponseBody
    public ResponseMsg updOrder(OrderInfo orderInfo){
        ResponseMsg rm = new ResponseMsg();
        boolean b = orderService.updOrder(orderInfo);
        if (b){
            rm.setResult("0000");
        }else{
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //跳转到订单详情界面
    @RequestMapping("orderDetailList")
    public String orderDetailList(String oid,HttpServletRequest request){
        OrderPojo orderPojo = orderService.queryOrder(oid);
        if(orderPojo!=null&&orderPojo.getOds()!=null){
            request.setAttribute("orderDetail",orderPojo.getOds());
            return "orderDetail-list";
        }else{
            return "error";
        }
    }

    //修改订单详情价格
    @RequestMapping("updOrderDetail")
    @ResponseBody
    public ResponseMsg updOrderDetail(OrderDetailsPojo orderDetailsPojo,HttpServletRequest request){
        ResponseMsg rm = new ResponseMsg();
        if (orderDetailsPojo.getOdprice()==orderDetailsPojo.getOldprice()){
            rm.setResult("0000");
        }else{
            boolean b = orderService.updOrderDetail(orderDetailsPojo);
            if(b){
                rm.setResult("0000");
            }else{
                rm.setResult("0001");
                rm.setResultMsg("修改失败");
            }
        }
        return rm;
    }

}
